<template>
  <div class="dividePage">
        <div class="dividePages">
          <span class="totalNumber">共{{itemLength}}条</span>
          <span class="pageSize">
            <div class="select">
                <div class="input">
                    <input type="text" readonly="readonly" autocomplete="off" :value="this.placeholder" class="inputInner">
                    <span class="suffix">
                        <span>
                            <i class="suffixI" @click="showDown()">
                              <img :class="{'down':upordown==1}" src="../../assets/img/up.png">
                            </i>
                        </span> 
                    </span>
                </div>
                <div class="dropDown" :class="{'hide':upordown==1}">
                    <div class="scrollbar">
                        <div class="wrap" style="margin-bottom: -17px; margin-right: -17px;">
                            <ul class="wrapList" v-for="(pagesize,index) in pagesizes" 
                            :key="index">
                                <li class="dropdownItem" :class="{'selected': $store.state.select==index+1}" @click="choosethis(index)">
                                  <span>{{pagesize}}条/页</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
          </span>
          <button type="button" class="btnPrev" @click="prev()">
            <i>
              <img src="../../assets/img/left.png">
            </i>
          </button>
          <ul class="pager">
            <li 
              v-for="index of page" 
              :key="index" 
              :class="{'active':isIndex==index}"
               class="number"
               @click="jump(index)"
              >{{index+i}}</li>
          </ul>
          <button type="button" class="btnNext" @click="next()">
            <i>
              <img src="../../assets/img/right.png">
            </i>
          </button>
          <span class="jumpTo">
            前往
            <div class="jumpToPage">
                <input
                    type="number"
                    autocomplete="off"
                    maxlength="1"
                    @keyup="jumpTo()"
                    ref="getValue"
                    oninput="if(value>9){value=9}else{value=value.replace(/[^\d]/g,'')}if(value.indexOf(0)==0){value=1}"
                >
            </div>
            页
          </span>
        </div>
      </div>
</template>

<script>
export default {
    name: 'DividePage',
    props:['changePage','itemLength','pagesizes','maxsize'],
    data() {
        return {
            upordown:1,
            isIndex:1,
            // select:1,
            // number:5,
            i:0,
            placeholder:`${this.pagesizes[0]}条/页`,
        }
    },
    computed:{
        number(){
            // console.log(this.$store.state.select)
            var a = this.$store.state.select - 1
            // console.log("page",this.pagesizes[a])
            if(this.itemLength == undefined){
                this.itemLength = 0
            }
            if(this.itemLength%(this.$store.state.select*this.pagesizes[a]) == 0){
                return Math.floor(this.itemLength/(this.$store.state.select*this.pagesizes[a]))
            }else{
                return (Math.floor(this.itemLength/(this.$store.state.select*this.pagesizes[a]))+1)
            }
        },
        page(){
            if(this.number<=5){
                return this.number
            }else{
                return 5
            }
        },
        pageNumber(){
            return this.isIndex + this.i
        },
    },
    watch:{
        pageNumber:{
            immediate:true,
            deep:true,
            handler(newValue,oldValue){
                console.log(newValue,oldValue)
                this.do(newValue)
            }
        }
    },
    methods:{
        do(value){
            this.changePage(value)
        },
        showDown(){
            if(this.upordown == 1){
                this.upordown = 0
            }else{
                this.upordown = 1
            }
        },
        choosethis(a){
            this.$store.dispatch('chooseit',a+1)
            console.log(this.pagesizes[a])
            console.log(this.pagesizes[0])
            this.placeholder = `${this.pagesizes[a]}条/页`
            this.changePage(1)
        },
        jump(a){
            console.log(a)
            this.isIndex = a
        },
        prev(){
            if(this.isIndex + this.i > 1){
                if(this.i>0){
                    this.i = this.i - 1
                }else{
                    this.isIndex = this.isIndex - 1
                }
            }else{
                alert("页数不能小于1!!!")
            }
        },
        next(){
            if(this.isIndex+this.i < this.number){
                if(this.isIndex<this.maxsize){
                    this.isIndex = this.isIndex + 1
                }else{
                    this.i =this.i + 1
                }
            }else{
                alert("后面没有信息了!!!")
            }
        },
        jumpTo(){
            if(this.$refs.getValue.value != '' && this.$refs.getValue.value<=this.number&&this.$refs.getValue.value>0){
                if(this.$refs.getValue.value>this.maxsize){
                    this.isIndex = this.maxsize
                    this.i = this.$refs.getValue.value - this.maxsize
                }else{
                    this.isIndex = this.$refs.getValue.value
                }
            }else{
                alert("请输入正确的格式")
            }
        },
    }
}
</script>

<style lang="less" scoped>
.dividePage{
    width: 100%;
    height: 25px;
    margin-bottom: 10px;
    padding: 10px 40px!important;
    box-sizing:border-box;
    .dividePages{
        white-space: nowrap;
        padding: 2px 5px;
        color: #303133;
        font-weight: 700;
        float: right;
        .totalNumber{
           font-size: 16px;
           margin-right: 5px;
        }
        .pageSize{
            margin: 0 10px 0 0;
            font-weight: 400;
            color: #606266;
            .select{
                display: inline-block;
                position: relative;
                .input{
                    width: 100px;
                    margin: 0 5px;
                    display: block;
                    font-size: 12px;
                    margin-right: 36px;
                    .inputInner{
                        width: 100%;
                        border-radius: 3px;
                        font-size: 13px;
                        height: 28px;
                        line-height: 28px;
                        cursor: pointer;
                        border: 1px solid #dcdfe6;
                        color: #606266;
                        padding: 0 15px;
                        outline: none;
                    }
                    .suffix{
                        position: absolute;
                        height: 100%;
                        top: 0;
                        text-align: center;
                        color: #c0c4cc;
                        pointer-events: none;
                        vertical-align: middle;
                        span{
                            pointer-events: all;
                        }
                        .suffixI{
                            color: #c0c4cc;
                            font-size: 14px;
                            transition: all 0.6s;
                            cursor: pointer;
                            line-height: 28px;
                            width: 25px;
                            height: 100%;
                            text-align: center;
                            margin-left: -30px;
                            img{
                                margin-top: 8px;
                                width: 12px;
                                height: 12px;
                                transition: all 0.6s;
                                margin: 6px;
                            }
                            .down{
                                transform: rotateZ(-180deg);
                            }
                        }
                    }
                }
                .hide{
                    display:none; 
                    min-width: 110px;
                }
                .dropDown{
                    position: absolute;
                    z-index: 1001;
                    border: 1px solid #dfe4ed;
                    border-radius: 4px;
                    background-color: #fff;
                    box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
                    box-sizing: border-box;
                    margin: 5px 0 5px 5px;
                    .scrollbar{
                        overflow: hidden;
                        position: relative;
                        .wrap{
                            overflow: scroll;
                            height: 100%;
                            max-height: 274px;
                            .wrapList{
                                list-style: none;
                                padding: 6px 0;
                                margin: 0;
                                box-sizing: border-box;
                                .dropdownItem{
                                    font-size: 14px;
                                    padding: 0 20px;
                                    position: relative;
                                    white-space: nowrap;
                                    overflow: hidden;
                                    text-overflow: ellipsis;
                                    color: #606266;
                                    height: 34px;
                                    line-height: 34px;
                                    box-sizing: border-box;
                                    cursor: pointer;
                                }
                                .selected{
                                    color: #1890ff;
                                    font-weight: 700;
                                }
                            }
                        }
                    }
                }
            }
        }
        .btnPrev,.btnNext{
            background-color: #f4f4f5;
            color: #606266;
            min-width: 30px;
            min-height: 30px;
            border-radius: 2px;
            border: none;
            cursor: pointer;
            text-align: center;
            img{
                margin-top: 3px;
                width: 12px;
                height: 12px;
                margin: 7px;
            }
        }
        // .btnPrev{
        //     margin: 0 3px 0 25px;
        // }
        .btnNext{
            margin: 0 3px;
        }
        .pager{
            user-select: none;
            list-style: none;
            display: inline-block;
            vertical-align: top;
            font-size: 0;
            padding: 0;
            margin: 0;
            .active{
                background-color: #1890ff !important;
                color: #fff;
            }
            .number{
                margin: 1px 3px 0 3px;
                min-width: 30px;
                border-radius: 2px;
                padding: 0 4px;
                background: #fff;
                vertical-align: top;
                display: inline-block;
                font-size: 13px;
                height: 28px;
                line-height: 28px;
                box-sizing: border-box;
                text-align: center;
                cursor: pointer;
            }
        }
        .jumpTo{
            margin-left: 24px;
            margin-right: -12px;
            font-weight: 400;
            color: #606266;
            display: inline-block;
            font-size: 13px;
            min-width: 35.5px;
            height: 28px;
            line-height: 28px;
            vertical-align: top;
            box-sizing: border-box;
            .jumpToPage{
                width: 50px;
                font-size: 14px;
                line-height: 18px;
                padding: 0 2px;
                height: 28px;
                text-align: center;
                margin: 0 2px;
                box-sizing: border-box;
                border-radius: 3px;
                display: inline;
                input{
                    width: 50px;
                    height: 28px;
                    line-height: 36px;
                    padding: 0 3px;
                    text-align: center;
                    background-color: #fff;
                    border-radius: 4px;
                    border: 1px solid #dcdfe6;
                    box-sizing: border-box;
                    color: #606266;
                    display: inline-block;
                    font-size: inherit;
                    outline: none;
                }
            }
        }
    }
}
</style>